package com.bajiao.app.controller.store;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.bajiao.app.base.AppBaseController;
import com.bajiao.common.api.vo.RestResult;
import com.bajiao.common.constant.CommonConstant;
import com.bajiao.common.constant._Whole;
import com.bajiao.common.exception.DataHandleException;
import com.bajiao.common.menu.ResultEnum;
import com.bajiao.common.util.PasswordUtil;
import com.bajiao.entity.store.Store;
import com.bajiao.entity.store.StoreUser;
import com.bajiao.entity.user.User;
import com.bajiao.service.store.StoreService;
import com.bajiao.service.store.StoreUserService;
import com.bajiao.third.utils.AliSmsUtils;
import com.bajiao.vo.store.StoreUserDetails;
import com.bajiao.vo.store.StoreUserInfo;
import com.bajiao.vo.user.UserDetails;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Slf4j
@Api(tags = "微众通用户管理")
@RestController
@RequestMapping("/api/v1/wzt/user")
public class RestStoreUserController extends AppBaseController {

    @Resource
    private StoreUserService storeUserService;
    @Resource
    private StoreService storeService;

    @PostMapping(value = "/cipher/login")
    @ApiOperation(value = "密码登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mobile", value = "手机号", dataType = "String", required = true),
            @ApiImplicitParam(name = "cipher", value = "密码", dataType = "String", required = true)
    })
    public RestResult<StoreUserDetails> wztCipherLogin(@RequestParam String mobile,
                                                       @RequestParam String cipher) {
        //@create by FanJq 2024/12/25 17:05 获取用户信息
        StoreUser storeUser = storeUserService.getByAccount(mobile);
        //@create by FanJq 2024/7/8 15:23 未发现绑定信息返回特定code让前端跳转注册页面
        if (ObjectUtil.isNull(storeUser))
            return fail(ResultEnum.NO_BIND);
        String cipherText = PasswordUtil.encrypt(cipher, "", storeUser.getSalt());
        if (!StrUtil.equals(storeUser.getPassword(), cipherText))
            throw new DataHandleException("密码错误");
        Store store = storeService.getById(storeUser.getStoreId());
        if (store.getDelFlag() == _Whole.ONE)
            throw new DataHandleException("店铺不存在, 登录失败");
        if (store.getStatus() == _Whole.ZERO)
            throw new DataHandleException("店铺已关闭, 登录失败");
        StoreUserDetails details = createUserTokenMap(tokenBuild(storeUser), storeUser);
        details.getUserInfo().setStoreLogo(store.getCover());
        details.getUserInfo().setStoreTitle(store.getTitle());
        details.getUserInfo().setStoreStatus(store.getStatus());
        return success(details);
    }

    @PostMapping(value = "/secretFree/login")
    @ApiOperation(value = "免密登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mobile", value = "手机号", dataType = "String", required = true)
    })
    public RestResult<StoreUserDetails> wztSecretFreeLogin(@RequestParam String mobile) {
        //@create by FanJq 2024/12/25 17:05 获取用户信息
        StoreUser storeUser = storeUserService.getByAccount(mobile);
        //@create by FanJq 2024/7/8 15:23 未发现绑定信息返回特定code让前端跳转注册页面
        if (ObjectUtil.isNull(storeUser))
            return fail("非商家身份，登录失败");
        Store store = storeService.getById(storeUser.getStoreId());
        if (store.getDelFlag() == _Whole.ONE)
            throw new DataHandleException("店铺不存在, 登录失败");
        if (store.getStatus() == _Whole.ZERO)
            throw new DataHandleException("店铺已关闭, 登录失败");
        StoreUserDetails details = createUserTokenMap(tokenBuild(storeUser), storeUser);
        details.getUserInfo().setStoreLogo(store.getCover());
        details.getUserInfo().setStoreTitle(store.getTitle());
        details.getUserInfo().setStoreStatus(store.getStatus());
        return success(details);
    }

    @PostMapping(value = "/forget/cipher")
    @ApiOperation(value = "忘记密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mobile", value = "手机号", dataType = "String", required = true),
            @ApiImplicitParam(name = "captcha", value = "验证码", dataType = "String", required = true),
            @ApiImplicitParam(name = "cipher", value = "密码", dataType = "String", required = true)
    })
    public RestResult forgetCipher(@RequestParam String mobile,
                                    @RequestParam String captcha,
                                    @RequestParam String cipher) {
        //@create by FanJq 2024/12/25 17:18 校验验证码
        AliSmsUtils.check(mobile, CommonConstant.SMS_EVENT_FORGET, captcha);
        //@create by FanJq 2024/12/25 17:05 获取用户信息
        StoreUser storeUser = storeUserService.getByAccount(mobile);
        //@create by FanJq 2024/7/8 15:23 校验是否注册
        if (ObjectUtil.isNull(storeUser))
            return fail(ResultEnum.NO_BIND);
        storeUserService.modifyCipher(storeUser, cipher);
        return success();
    }

    @PostMapping(value = "/info")
    @ApiOperation(value = "用户信息")
    public RestResult<StoreUserInfo> getWztUserInfo(@ModelAttribute("storeUser") StoreUser storeUser) {
        if (ObjectUtil.isNull(storeUser))
            return tokenOverdue();
        StoreUserInfo userVO = storeUserService.getUserInfo(storeUser);
        Store store = storeService.getById(storeUser.getStoreId());
        if (ObjectUtil.isNotNull(store)) {
            userVO.setStoreLogo(store.getCover());
            userVO.setStoreTitle(store.getTitle());
            userVO.setStoreStatus(store.getStatus());
            userVO.setStoreSource(store.getType());
        }
        return success(userVO);
    }


    /**
     * @Description
     * 组装登录返回数据
     * @date 2024/12/26 10:33
     * @author FanJq
     * @param token
     * @param user
     * @return {@link StoreUserDetails}
    */
    public StoreUserDetails createUserTokenMap(String token, StoreUser user) {
        //返参
        StoreUserInfo userVO = storeUserService.getUserInfo(user);
        return new StoreUserDetails(userVO, token);
    }
}
